/*
 * Preferences.java
 * Describe SWF
 *
 * A browser for Flash files.
 * Copyright (c) 2001-2006 Flagstone Software Ltd. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * For more information please contact:
 * 
 * Stuart MacKay
 * Flagstone Software Ltd
 * 92 High Street
 * Wick, Caithness KW1 4LY
 * Scotland
 * 
 * www.flagstonesoftware.com
 */
package com.flagstone.describe;

import java.util.*;
import java.util.logging.*;
import java.io.*;
import java.net.URL;

final class Preferences 
{
    private static Preferences sharedInstance = null;
    
    static synchronized Preferences getInstance()
    {
        if (sharedInstance == null)
            sharedInstance = new Preferences();
        
        return sharedInstance;
    }

    private File userDir = null;
    private File userFile = null;
    
    private Properties system = null;
    private Properties session = null;
    private Properties user = null;

    private Preferences()
    {
        system = new Properties();
        session = new Properties();
        user = new Properties();
        
        userDir = new File(System.getProperty("user.home"), "."+App.AppName);
        userFile = new File(userDir, App.AppName+".pref");
        
        loadPreferencesFromURL(system, getClass().getResource("/"+App.AppName+".properties"));
        loadPreferencesFromFile(user, userFile);
    }
    
    Object get(String aKey)
    {
        Object preference = null;
        
        if (user.containsKey(aKey))
            preference = user.get(aKey);
        else if (session.containsKey(aKey))
            preference = system.get(aKey);
        else if (system.containsKey(aKey))
            preference = system.get(aKey);
        else
        {
            String message = App.getInstance().localizedString("Error.MissingPreference");
            Logger.getLogger(App.AppName).warning(message);
        }          
        return preference;
    }
    
    void setUserPreference(String key, String value)
    {
        user.put(key, value);
    }
    void setSessionPreference(String key, String value)
    {
        session.put(key, value);
    }
    void savePreferences()
    {
        savePreferencesToFile(user, userFile);
    }
    
    private void loadPreferencesFromURL(Properties properties, URL url)
    {
        try 
        {
            InputStream stream = url.openStream();
            properties.load(stream);
            stream.close();
        }
        catch(Exception e) 
        {
            String message = App.getInstance().localizedString("Error.LoadPreferences", url.toString());
            Logger.getLogger(App.AppName).warning(message);
        }
    }
    private void loadPreferencesFromFile(Properties properties, File file)
    {
        try 
        {
            if (file.exists() && file.canRead())
            {
                FileInputStream fileStream = new FileInputStream(file);
                properties.load(fileStream);
                fileStream.close();
            }
        }
        catch(Exception e) 
        {
            String message = App.getInstance().localizedString("Error.LoadPreferences", file.getPath());
            Logger.getLogger(App.AppName).warning(message);
        }
    }   
    void savePreferencesToFile(Properties properties, File file)
    {
        try 
        {
            FileOutputStream fileStream = new FileOutputStream(file);
            properties.store(fileStream, null);
            fileStream.close();
        }
        catch(Exception e) 
        { 
            String message = App.getInstance().localizedString("Error.SavePreferences", file.getPath());
            Logger.getLogger(App.AppName).warning(message);
        }
    }
}
